# -*- encoding=utf8 -*-
        
__author__ = "Bryan.Chen"
__desc__=""
        
from airtest.core.api import *
        
auto_setup(__file__)
        
from poco.drivers.std import StdPoco
poco = StdPoco()
        
using("../FlowTest.air")
import FlowTest

import random

class Task_AddAddress(FlowTest.BaseTestCase):
    def __init__(self):
        print("%s __init__" % (__file__))
        
    def runTest(self, args):
        print("%s runTest" % (__file__))
        btnMoreMission = poco("more_mission")
        if btnMoreMission.exists():
            self.clickButton(btnMoreMission)

        try:
            poco("MissionCenterLayer").wait_for_appearance()
        except:
            assert_equal(False, True, "任务中心正常弹出")
            return False

        missionCenterLayer = poco("MissionCenterLayer")
        titleBar = missionCenterLayer.offspring("titile_bar")

        # 打开兑换中心
        btnOpenTip = titleBar.offspring("open_tip")
        self.clickButton(btnOpenTip)

        try: 
            poco("ExchangeCenterLayer").wait_for_appearance()
        except:
            assert_equal(False, True, "兑换中心正常打开")
            return False

        exchangeCenterLayer= poco("ExchangeCenterLayer")
        btnAddress = exchangeCenterLayer.offspring("address_click")
        if btnAddress.exists():
            self.clickButton(btnAddress)

            exchangeAddressLayer = exchangeCenterLayer.offspring("ExchangeAddressLayer")
            if not exchangeAddressLayer.exists():
                assert_equal(False, True, "兑换中心增加地址弹窗正常打开")
                return False

            btnAddAddress = exchangeAddressLayer.offspring("no_address_btn")
            if not btnAddAddress.exists():
                btnAddAddress = exchangeAddressLayer.offspring("new_address_btn")
            self.clickButton(btnAddAddress)

            exchangeEditAddressLayer = exchangeAddressLayer.offspring("ExchangeEditAddressLayer")

            txtName = exchangeEditAddressLayer.offspring("name_input")
            self.clickButton(txtName)
            text("张三")

            txtPhone = exchangeEditAddressLayer.offspring("phone_input")
            self.clickButton(txtPhone)
            text("13800138000")

            btnSelectArea = exchangeEditAddressLayer.offspring("select_area_btn")
            self.clickButton(btnSelectArea)
            
            try:
                # 等待切换地区界面显示
                poco("LocationView").wait_for_appearance()
            except:
                assert_equal(False, True, "地区选择界面弹出正常")
                return False

            # 获取地区选择界面
            locationView = poco("LocationView")
            if not locationView.exists():
                return False
            
            # 获取热门地区
            hotItems = locationView.offspring("button_ad_temp")
            strLocation = ""
            if len(hotItems) > 0:
                # 生成随机数，随机选一个地区切换
                n = random.randint(0, len(hotItems) - 1)
                strLocation = hotItems[n].get_text()
                self.clickButton(hotItems[n])
            # 等待1s，待地区选择界面完全隐藏再操作
            sleep(1.0)

            txtAddress = exchangeEditAddressLayer.offspring("address_input")
            self.clickButton(txtAddress)
            text("xx省xx市xx路xx号101(自动化测试)")

            btnSaveBtn = exchangeEditAddressLayer.offspring("save_btn")
            self.clickButton(btnSaveBtn)

            # 获取提示信息，并进行比对是否“修改成功”
            toastView = poco("ToastView")        
            if toastView.exists():
                txtContent = toastView.offspring("text_content").get_text()
                assert_equal(txtContent, "添加地址成功", "添加地址成功")

            btnClose = exchangeAddressLayer.offspring("back")
            self.clickButton(btnClose)

        btnClose = exchangeCenterLayer.offspring("titile_bar").child("back")
        if btnClose.exists():
            self.clickButton(btnClose)

        btnClose = titleBar.child("back")
        if btnClose.exists():
            self.clickButton(btnClose)

        return True
        
_instance = Task_AddAddress()
runTest = _instance.runTest